﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Common;
using Husb.Data;
using Husb.DataUtil;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching;

using CentralOffice.PublicAffair.Data;

using PostReplyTable = CentralOffice.PublicAffair.Data.PostReplyDataSet.PostReplyDataTable;
using PostReplyRow = CentralOffice.PublicAffair.Data.PostReplyDataSet.PostReplyRow;



namespace CentralOffice.PublicAffair.DataAccess
{
    public class PostReplyDataAdapter : DataAccessAdapter<PostReplyDataSet, PostReplyTable, PostReplyRow>
    {
        private const string UpdateStatusStoredProc = "Tasks.Tasks_UpdateTaskStatus";
        private const string SelectTaskAttendmentsByIdStoredProc = "Tasks.Tasks_SelectTaskAttendmentsById";
        private const string SelectTaskByEmployeeStoredProc = "Tasks.Tasks_SelectTaskByEmployee";

        private const string SelectFirstReply = "SELECT TOP 1 Id, ReplyTitle, ReplyText, PostId, ReplyBy, ReplyDate, Attachments FROM PublicAffairs.PostReply WHERE PostId = @PostId ORDER BY ReplyDate";

        public PostReplyDataAdapter()
        {
            this.TableName = "PostReply";
            this.MasterFieldName = "PostId";

            GetAllStoredProc = "PublicAffairs.PostReply_SelectPostRepliesAll";
            InsertStoredProc = "PublicAffairs.PostReply_InsertPostReply";
            UpdateStoredProc = "PublicAffairs.PostReply_UpdatePostReply";
            DeleteStoredProc = "PublicAffairs.PostReply_DeletePostReply";

            GetByIdStoredProc     = "PublicAffairs.PostReply_SelectPostReply";
            GetByNameStoredProc   = "";
            GetPagedStoredProc    = "PublicAffairs.PostReply_SelectPostRepliesPaged";
            GetRowCountStoredProc = "PublicAffairs.PostReply_SelectPostRepliesRowCount";

            GetByMasterIdStoredProc = "PublicAffairs.PostReply_SelectPostRepliesByPostId";
            FindStoredProc = "PublicAffairs.Tasks_SelectTasksDynamic";
            LogicalDeleteStoredProc = "PublicAffairs.Tasks_DeleteTaskLogical";
        }

        protected override void PopulateInsertParameters(Database db, DbCommand cmd)
        {
            db.AddInParameter(cmd, "Id", DbType.Guid, "Id", DataRowVersion.Current);
            db.AddInParameter(cmd, "ReplyTitle", DbType.AnsiString, "ReplyTitle", DataRowVersion.Current);
            db.AddInParameter(cmd, "ReplyText", DbType.String, "ReplyText", DataRowVersion.Current);
            db.AddInParameter(cmd, "PostId", DbType.Guid, "PostId", DataRowVersion.Current);
            db.AddInParameter(cmd, "ReplyBy", DbType.AnsiString, "ReplyBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "ReplyDate", DbType.DateTime, "ReplyDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "Attachments", DbType.AnsiString, "Attachments", DataRowVersion.Current);
        }

        public PostReplyRow GetFirstReply(Guid postId)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("PostId", DbType.Guid, postId));
            PostReplyDataSet ds = Husb.DataUtil.DataAccessUtil.GetDataSet<PostReplyDataSet>(SelectFirstReply, parameters, this.TableName, false, false, null);

            PostReplyTable dt = ds.PostReply;
            if(dt.Rows.Count > 0)
            {
                return dt.Rows[0] as PostReplyRow;
            }
           
            return null;
        }
    }
}
//where += DataAccessUtil.GetPartWhereCondition("Id", this);
//where += DataAccessUtil.GetPartWhereCondition("ReplyTitle", this);
//where += DataAccessUtil.GetPartWhereCondition("ReplyText", this);
//where += DataAccessUtil.GetPartWhereCondition("PostId", this);
//where += DataAccessUtil.GetPartWhereCondition("ReplyBy", this);
//where += DataAccessUtil.GetPartWhereCondition("ReplyDate", this);
//where += DataAccessUtil.GetPartWhereCondition("Attachments", this);

//lblId.Text = postReply.Id;
//lblReplyTitle.Text = postReply.ReplyTitle;
//lblReplyText.Text = postReply.ReplyText;
//lblPostId.Text = postReply.PostId;
//lblReplyBy.Text = postReply.ReplyBy;
//lblReplyDate.Text = postReply.ReplyDate;
//lblAttachments.Text = postReply.Attachments;

//txtId.Text = postReply.Id;
//txtReplyTitle.Text = postReply.ReplyTitle;
//txtReplyText.Text = postReply.ReplyText;
//txtPostId.Text = postReply.PostId;
//txtReplyBy.Text = postReply.ReplyBy;
//txtReplyDate.Text = postReply.ReplyDate;
//txtAttachments.Text = postReply.Attachments;

//postReply.Id = txtId.Text;
//postReply.ReplyTitle = txtReplyTitle.Text;
//postReply.ReplyText = txtReplyText.Text;
//postReply.PostId = txtPostId.Text;
//postReply.ReplyBy = txtReplyBy.Text;
//postReply.ReplyDate = txtReplyDate.Text;
//postReply.Attachments = txtAttachments.Text;
